q#!/bin/python
# Takes the spreadsheet stored as csv from 
# http://spreadsheets.google.com/ccc?key=pzg-CZ_JbcaAwrgaqZt24MA&inv=caxqueiroz@gmail.com&t=8110409210561487889&guest
# and converts it into Localizable.strings files.
#
#  Created by Dave MacLachlan on 2008/01/06.
#  Copyright 2007-2008 Google Inc. All rights reserved.
# 
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License.  You may obtain a copy
# of the License at
# 
# http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
# License for the specific language governing permissions and limitations under
# the License.
#

import csv
import os
import sys
import codecs

def main():	
	table = []
	reader = csv.reader(open(sys.argv[1], "rb"))
	for row in reader:
		if len(row) > 0:
			table.append(row)
	languageCount = len(table[0]) - 1
	
	for i in range(1, languageCount + 1):
		if table[3][i].lower() == "finished":
			os.chdir(sys.argv[2])
			lprojName = table[2][i] + ".lproj"
			try:
				os.mkdir(lprojName)
			except:
				pass
			os.chdir(lprojName)
			theFile = codecs.open("Localizable.strings", mode="w", encoding="utf-16")
			theFile.write(unicode("/*\r\tLocalized Strings For Statz\r\tLanguage: " + 
                            table[0][i] + "\r\tTranslator: " + table[1][i] + 
                            "\r  Copyright 2007 Google Inc. All rights reserved.\r" +
                            "\r  Licensed under the Apache License, Version 2.0 (the \"License\"); you may not" +
                            "\r  use this file except in compliance with the License.  You may obtain a copy" +
                            "\r  of the License at\r" +
                            "\r  http://www.apache.org/licenses/LICENSE-2.0\r" +
                            "\r  Unless required by applicable law or agreed to in writing, software" +
                            "\r  distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT" +
                            "\r  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the" +
                            "\r  License for the specific language governing permissions and limitations under" +
                            "\r  the License.\r" +
                            "*/\r\r", 'utf-8'))
			for j in range(4, len(table)):
				if i < len(table[j]):
					english = table[j][1]
					value = ""
					if len(table[j][i]):
						value = table[j][i]
					else:
						value = table[j][1]
					value = value.replace("\"", "\\\"")
					english = english.replace("\"", "\\\"")
					theFile.write(unicode("\"" + english + "\" = \"" + value + "\";\r", 'utf-8'))
		

if __name__ == '__main__':
  main()
